package com.example.spookydoors;

import java.util.Random;

import android.os.Bundle;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class GameActivity extends Activity {

	public Random generator=new Random();
	public int score = 0;
	public TextView scoreBox;
	public int life = 3;
	public TextView livesBox;
	
	private ImageButton door;
	private ImageButton door1;
	private ImageButton door2;
	private ImageButton door3;
	private int mshortDur, mLongDur;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_activity);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		scoreBox = (TextView) findViewById(R.id.scoreBox);
		livesBox = (TextView) findViewById(R.id.livesBox);
		door1 = (ImageButton) findViewById(R.id.door1);
		door2 = (ImageButton) findViewById(R.id.door2);
		door3 = (ImageButton) findViewById(R.id.door3);
		
		mshortDur = getResources().getInteger(
                android.R.integer.config_shortAnimTime);
		mLongDur = getResources().getInteger(
                android.R.integer.config_longAnimTime);
		//mshortDur = 1500;

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.game, menu);
		return true;
	}
	
	public void changeScore(View v){		
		door = (ImageButton) v;
		crossfade();
	}
	
	private void crossfade() {
		
		door.setVisibility(View.VISIBLE);
		door.setAlpha(1f);
		door1.setClickable(false);
		door2.setClickable(false);
		door3.setClickable(false);
		
		door.animate()
        .alpha(.2f)
        .setDuration(mLongDur)
        .setListener(new AnimatorListenerAdapter() 
           {
        	@Override
        	public void onAnimationEnd(Animator animation) 
        	 {
        		//door.setImageResource(R.drawable.door2);
        		int num;
        		num = finishTurn();
        		if(num ==0)
        		{
        			door.setImageResource(R.drawable.door_ghost);
        		} else {
        			door.setImageResource(R.drawable.candy_door);
        		}
        		door.animate().alpha(1f).setDuration(2000)
        			.setListener(new AnimatorListenerAdapter()
        			{
        				@Override
        				public void onAnimationEnd(Animator animation) 
        				{
            				
                    		door.animate()
                    		.alpha(.3f)
                    		.setDuration(mshortDur)
                    		.setListener(new AnimatorListenerAdapter() 
                    		{
                    			@Override
                    			public void onAnimationEnd(Animator animation) 
                    			{
                    				door.setImageResource(R.drawable.door_300x430);
                    				door.animate()
                    				.alpha(1f)
                    				.setDuration(mshortDur)
                    				.setListener(new AnimatorListenerAdapter()
                    				{
                    					@Override
                    					public void onAnimationEnd(Animator animation)
                    					{
                                    		door1.setClickable(true);
                            				door2.setClickable(true);
                            				door3.setClickable(true);
                    						//door.setImageResource(R.drawable.door_300x430);                						
                    					}
                    				});
                    			}
                    	    });
                       }
                    });
        		
               }  
           });
	}
	
	AnimatorListenerAdapter myListener = new AnimatorListenerAdapter() 
	{
		@Override
		public void onAnimationEnd(Animator animation) 
		{
			door.setImageResource(R.drawable.door_300x430);
		}
	};
	
	private int finishTurn() {
		int num = generator.nextInt(6);
		score += num;
		scoreBox.setText("Score: " + score);
		if (num == 0){
			life --;
		}
		livesBox.setText("Lives: " + life);
		if (life == 0){
			Intent i = new Intent(this, GameOver.class);
			i.putExtra("score",score);
	    	startActivity(i);
	    	finish();
		}	
		return num;
	
	}

}
